#include<bits/stdc++.h>
using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	vector<int>v[n + 10];
	bool flag = false;
	for(int i = 1; i <= m; i ++ ) {
		int x, y;
		cin >> x >> y;
		v[x].push_back(y);
		if(y == 1) {
			flag = true;
		}
	}
	if(!flag) {
		cout << -1 << endl;
		return 0;
	}
	int ret[n + 10];
	memset(ret, 0x3f, sizeof ret);
	ret[1] = 0;
	queue<int>q;
	q.push(1);
	bool p = false;
	
	while(q.size()) {
		int d = q.front();
		//cout << "d = " << d << endl;
		q.pop();
		for(int i = 0; i < v[d].size(); i ++ ) {
			int j = v[d][i];
			//cout << "j = " << j << endl;
			
			if(ret[j] > ret[d] + 1 || j == 1) {
				if(j  == 1) {
					if(p == false) {
						ret[j] = ret[d] + 1;
						p = true;
					} else {
						ret[j] = min(ret[j], ret[d] + 1);
					}
				} else {
					ret[j] = ret[d] + 1;
				}
				q.push(j);
			}
		//	cout << "ret[j] = " << ret[j] << endl;
		} 
	}
	cout << (ret[1] == 0 ? -1 : ret[1] ) << endl;
	return 0;
}

